package com.ict.web.controller.yzq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.ict.common.annotation.Log;
import com.ict.common.enums.BusinessType;
import com.ict.common.utils.SecurityUtils;
import com.ict.web.constant.Constants;
import com.ict.web.domain.OrganizationManagement;
import com.ict.web.dto.*;
import com.ict.web.mapper.ReviewTaskMapper;
import com.ict.web.service.IOrganizationManagementService;
import com.ict.web.service.IReviewTaskService;
import com.ict.web.utils.pdf.RoleUtil;
import com.ict.web.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ict.common.core.controller.BaseController;
import com.ict.common.core.domain.AjaxResult;
import com.ict.common.core.page.TableDataInfo;
import javax.validation.Valid;
import java.util.List;

/**
 * 评审任务Controller
 *
 * @author ruoyi
 * @date 2022-02-16
 */
@Api(tags = "审核任务")
@RestController
@RequestMapping("/system/task")
public class ReviewTaskController extends BaseController
{
    @Autowired
    private IReviewTaskService reviewTaskService;
    @Autowired
    private ReviewTaskMapper reviewTaskMapper;
    @Autowired
    private IOrganizationManagementService organizationManagementService;

    /**
     * 查看评审任务详情（项目id查）
     */
    @GetMapping("/detailByPro/{projectId}")
    @ApiOperation(value ="查看评审任务详情（项目id）")
    public ReviewTaskDetailVo detailByProId(@PathVariable Long projectId)
    {
        return reviewTaskService.reviewTaskDetailByProId(projectId);
    }
    /**
     * 修改评审任务
     */
    @GetMapping("/edit")
    @ApiOperation(value ="修改评审任务")
    public AjaxResult edit(ReviewTaskEditDto dto)
    {
        return toAjax(reviewTaskService.edit(dto));
    }
    /**
     * 查看评审任务详情（评审任务id查）
     */
    @GetMapping("/detailByTask/{reviewTaskId}")
    @ApiOperation(value ="查看评审任务详情（任务id）")
    public ReviewTaskDetailVo detailByTaskId(@PathVariable Long reviewTaskId)
    {
        return reviewTaskService.reviewTaskDetailByTaskId(reviewTaskId);
    }
    /**
     * 评审任务详情（判断更换专家需求）
     */
    @GetMapping("/detailTask/{reviewTaskId}")
    @ApiOperation(value ="评审任务详情（判断更换专家需求）")
    public ReviewTaskDetailVo detailTask(@PathVariable Long reviewTaskId)
    {
        return reviewTaskService.detailTask(reviewTaskId);
    }
    /**
     * 查看评审任务详情（年度计划id查）
     */
    @GetMapping("/detailByPlan/{planId}")
    @ApiOperation(value ="查看评审任务详情（年度计划id）")
    public ReviewTaskDetailVo detailByPlanId(@PathVariable Long planId)
    {
        return reviewTaskService.reviewTaskDetailByPlanId(planId);
    }

    /**
     * 大数据局查询评审任务列表
     */
    @GetMapping("/reviewTL")
    @ApiOperation(value ="大数据局查询评审任务列表")
    public PageInfo selectReviewTaskList(ReviewTaskDto dto)
    {
        //大数据局角色和大数据局平台角色可以查看非三方的抽取管理信息
        if (RoleUtil.getUserRole(Constants.ROLE_TYPE_BIGDATA)||RoleUtil.getUserRole(Constants.ROLE_TYPE_BIGDATA_NORMAL)) {
            startPage();
            List<ReviewTaskListVo> list = reviewTaskService.selectReviewTaskList(dto);
            return new PageInfo(list);
            //三方机构查询三方抽取信息
        }else {
            QueryWrapper<OrganizationManagement> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("dept_id",SecurityUtils.getDeptId())
                    .eq("is_delete", Constants.DELETE_STATUS_NORMAL);
            OrganizationManagement organizationManagement = organizationManagementService.getOne(queryWrapper);
            QueryReviewTaskThirdDto queryReviewTaskThirdDto = new QueryReviewTaskThirdDto();
            BeanUtils.copyProperties(dto,queryReviewTaskThirdDto);
            queryReviewTaskThirdDto.setOrmId(organizationManagement.getOrmId());
            startPage();
            List<ReviewTaskThirdListVo> thirdListVo = reviewTaskMapper.selectThirdList(queryReviewTaskThirdDto,"1");
            return new PageInfo(thirdListVo);
        }
    }
    /**
     * 新增评审任务
     */
    @PostMapping("/add")
    @Log(title = "新增审核任务", businessType = BusinessType.INSERT)
    @ApiOperation(value ="新增审核任务")
    public AjaxResult addSave(@Valid @RequestBody InsertReviewTaskDto addDto)
    {
        return AjaxResult.success(reviewTaskService.insertReviewTask(addDto));
    }
    /**
     * 转第三方
     */
    @PreAuthorize("@ss.hasPermi('system:task:exchangeThird')")
    @PutMapping("/exchangeThird")
    @Log(title = "转第三方", businessType = BusinessType.UPDATE)
    @ApiOperation(value ="转第三方")
    public AjaxResult exchangeThird(String taskName,Long proId,Long ormId)
    {
        return toAjax(reviewTaskService.exchangeThird(taskName,proId,ormId));
    }
    /**
     * 专家申请取消
     */
    @PutMapping("/applyCancel")
    @Log(title = "专家申请取消", businessType = BusinessType.UPDATE)
    @ApiOperation(value ="专家申请取消")
    public AjaxResult applyCancel(Long reviewTaskId,String applyReason)
    {
        return toAjax(reviewTaskService.applyCancel(reviewTaskId,applyReason));
    }

    @PutMapping("/dealWith")
    @Log(title = "专家接受任务操作", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "专家接受任务操作")
    public AjaxResult dealWith(Long reviewTaskId,String receiveStatus,String refuseReason){
        return toAjax(reviewTaskService.dealWith(reviewTaskId,receiveStatus,refuseReason));
    }
    /**
     * 删除评审任务
     */
    @DeleteMapping("/delete/{reviewTaskId}")
    @Log(title = "删除评审任务", businessType = BusinessType.DELETE)
    @ApiOperation(value ="删除评审任务")
    public AjaxResult remove(@PathVariable Long reviewTaskId)
    {
        return toAjax(reviewTaskService.deleteReviewTaskByReviewTaskId(reviewTaskId));
    }
    /**
     * 专家删除对应的评审任务
     */
    @DeleteMapping("/delete")
    @Log(title = "专家删除对应的评审任务", businessType = BusinessType.DELETE)
    @ApiOperation(value ="专家删除对应的评审任务")
    public AjaxResult removeExpert(Long reviewTaskId,Long expertId)
    {
        return toAjax(reviewTaskService.removeExpert(reviewTaskId,expertId));
    }

    /**
     * 获取专家评审任务详细
     */
    @ApiOperation("查询专家审核任务详情")
    @GetMapping(value = "/expert/{reviewTaskId}")
    public AjaxResult getExpertReviewTaskInfo(@PathVariable("reviewTaskId") Long reviewTaskId,Long expertId)
    {
        return AjaxResult.success(reviewTaskService.selectExpertById(reviewTaskId,expertId));
    }
    /**
     * 查询专家评审任务列表
     */
    @ApiOperation("查询专家评审任务列表")
    @GetMapping("/expertList")
    public TableDataInfo expertList(QueryReviewTaskExpertDto queryExpert,Integer pageNum,Integer pageSize)
    {
        //startPage();
        List<ReviewTaskExpertListVo> list = reviewTaskService.selectExpertList1(queryExpert,pageNum,pageSize);
        return getDataTable(list);
    }

    /**
     * 获取评审任务详细（项目id）
     */
    @ApiOperation("查询审核(项目id)任务详情")
    @GetMapping(value = "/{proId}")
    public AjaxResult getReviewTaskInfo(@PathVariable("proId") Long proId)
    {
        return AjaxResult.success(reviewTaskService.getReviewTaskInfo(proId));
    }

    /**
     * 查询三方评审任务列表
     */
    @ApiOperation("查询三方评审任务列表")
    @GetMapping("/thirdList")
    public PageInfo thirdList(QueryReviewTaskThirdDto queryThird)
    {
        return reviewTaskService.selectThirdList(queryThird);
    }

    /**
     * 获取三方评审任务详细信息
     */
    @ApiOperation("查询三方审核任务详情")
    @GetMapping(value = "/third/{reviewTaskId}")
    public AjaxResult getThirdInfo(@PathVariable("reviewTaskId") Long reviewTaskId)
    {
        return AjaxResult.success(reviewTaskService.selectThirdById(reviewTaskId));
    }

    /**
     * 专家处理操作后的详情信息
     *
     */
    @GetMapping("/expertReceiveInfo/{reviewTaskId}")
    @ApiOperation(value = "专家处理操作后的详情信息")
    public AjaxResult getExpertReceiveInfo(@PathVariable("reviewTaskId") Long reviewTaskId,Long expertId) {
        return AjaxResult.success(reviewTaskService.receiveTaskDetail(reviewTaskId,expertId));
    }
    /**
     * 项目管理详情-查看评审任务详情
     */
    @ApiOperation("项目管理详情-查看评审任务详情")
    @GetMapping(value = "/reviewTaskVo/{approvalId}")
    public AjaxResult findReviewTaskVo(@PathVariable("approvalId") Long approvalId)
    {
        return AjaxResult.success(reviewTaskService.findReviewTaskVo(approvalId));
    }
}
